/**
 * @projectName interview_-assistant
 * @package org.example.service
 * @className org.example.service.FileConversionService
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package org.example.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.result.Result;
import org.example.service.FileConversionService;
import org.example.util.PdfConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * FileConversionService
 * @description
 * @author admin
 * @date 2025/4/15 10:52
 * @version TODO
 */
@Service
@Slf4j
public class FileConversionServiceImpl implements FileConversionService{

    @Autowired
    private PdfConverter pdfConverter;

    /**
     * 转换PDF文件为TXT文件
     * @return 转换结果
     */
    @Override
    public Result<String> convertPdfToTxt(String pdfPath, String txtPath) {
        try {
            // 检查PDF文件是否存在
            File pdfFile = new File(pdfPath);
            if (!pdfFile.exists()) {
                return Result.error("PDF文件不存在");
            }

            // 检查输出目录是否存在，不存在则创建
            File txtFile = new File(txtPath);
            File parentDir = txtFile.getParentFile();
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }

            // 执行转换
            boolean success = pdfConverter.convertPdfToTxt(pdfPath, txtPath);

            String content = readTxtFile(txtPath);
            System.out.println(content);

            if (success) {
                return Result.success("PDF转换TXT成功");
            } else {
                return Result.error("PDF转换TXT失败");
            }

        } catch (Exception e) {
            log.error("PDF转换异常", e);
            return Result.error("PDF转换异常: " + e.getMessage());
        }
    }

    @Override
    /**
     * 转换PDF文件为文本内容
     * @param pdfPath PDF文件路径
     * @return 转换结果
     */
    public Result<String> getPdfContent(String pdfPath) {
        try {
            String content = pdfConverter.convertPdfToText(pdfPath);
            System.out.println(content);
            return Result.success("PDF内容提取成功");
        } catch (IOException e) {
            log.error("PDF内容提取失败", e);
            return Result.error("PDF内容提取失败: " + e.getMessage());
        }
    }

    @Override
    /**
     * 转换上传的PDF文件为文本内容
     * @param pdfFile 上传的PDF文件
     * @return 转换结果
     */
    public Result<String> convertUploadedPdf(MultipartFile pdfFile) {
        try {
            String content = pdfConverter.convertPdfBytesToText(pdfFile.getBytes());
            System.out.println(content);
            return Result.success("PDF转换成功");
        } catch (IOException e) {
            log.error("PDF转换失败", e);
            return Result.error("PDF转换失败: " + e.getMessage());
        }
    }

    /**
     * 读取TXT文件内容
     */
    private String readTxtFile(String filePath) throws IOException {
        StringBuilder content = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
        }
        return content.toString();
    }
}